﻿using System.Collections.ObjectModel;
using System.Text.Json.Serialization;

namespace TimeSpan.Models;

public class SpanTasksConfig {

    public ObservableCollection<SpanTaskOption> Options { get; set; } = [];

    [JsonIgnore]
    public ObservableCollection<SpanTaskOption?> BindingObject { get; } = [];

    /// <summary>要始终控制数量在 8 个，所以实现此方法</summary>
    public ObservableCollection<SpanTaskOption?> GetBindingObject() {
        return [.. Options, .. Enumerable.Repeat<SpanTaskOption?>(null, 8 - Options.Count)];
    }

    /// <summary>要始终控制数量在 8 个，所以实现此方法</summary>
    public void Add(SpanTaskOption option) {
        if (Options.Count == 8) { throw new(); }
        Options.Add(option);
        InitBindingObject();
    }

    public void Delete(SpanTaskOption option) {
        Options.Remove(option);
        InitBindingObject();
    }

    /// <summary>初始化 BindingObject，绑定之前要调用此方法</summary>
    public void InitBindingObject() {
        BindingObject.Reset([.. Options, .. Enumerable.Repeat<SpanTaskOption?>(null, 8 - Options.Count)]);
    }

    internal void Replace(SpanTaskOption data, SpanTaskOption spanTaskOption) {
        Options.Replace(data, spanTaskOption);
        InitBindingObject();
    }
}
